#!/usr/local/bin/python
# -*- coding: utf-8 -*-
# time：2025.02.20


from sqlalchemy import Column, Integer, Float, and_, Text, Date
from sqlalchemy.orm import declarative_base

import init
from init import session
import toolkit

Base = declarative_base()

class CashFlowAlchemy(Base):
    '''
    类功能：现金流表数据库映射类
    '''

    __tablename__ = "cash_flow"

    id = Column(Integer, primary_key=True, autoincrement=True, comment="数据表id值")
    date_time = Column(Date, comment="数字格式的年份和月份时间")
    year = Column(Integer, comment="数据归属年份")
    month = Column(Integer, comment="数据归属月份")
    data_type = Column(Text, comment="标记属于[资产]或[权属]")

    category = Column(Text, comment="资产负债表的类目")
    path = Column(Text, comment="现金流表所属路径")
    inflow_value = Column(Float, comment="流入金额")
    outflow_value = Column(Float, comment="流出金额")
    difference_value = Column(Float, comment="净流量金额")



    def __init__(self,
                 year=2022,
                 month=1,
                 dataType="",
                 path="",
                 category="",
                 inflowValue=0.0,
                 outflowValue=0.0,
                 differenceValue=0.0
                 ):
        self.date_time = toolkit.setTargetDate(year, month)
        self.year = year
        self.month = month

        self.data_type = dataType
        self.path  = path
        self.category = category

        self.inflow_value = inflowValue
        self.outflow_value = outflowValue
        self.difference_value = differenceValue


    def AddCashFlowItem(self):
        '''
        函数功能：添加一条数据
        :return:
        '''

        session.add(self)
        session.commit()

    def UpdateCashFlowItem(self):
        '''
        函数功能：更新一条数据
        :return:
        '''


    def SearchCashFlowByMonthCategory(self, year, month, category):
        '''
        函数功能：以时间、类别为条件进行查询
        :param year:
        :param month:
        :param category:
        :return:
        '''

        cashFlowInfo=session.query(CashFlowAlchemy).filter(and_(CashFlowAlchemy.year == year,
                                                                        CashFlowAlchemy.month == month,
                                                                        CashFlowAlchemy.category == category,
                                                                          )).all()

        return cashFlowInfo


    def DelCashFlowByMonth(self, year, month):
        '''
        函数功能，删除指定月份的张博数据
        :param year:
        :param month:
        :return:
        '''

        deleteCashFlowInfo = session.query(CashFlowAlchemy).filter(and_(CashFlowAlchemy.year==year,
                                                                     CashFlowAlchemy.month==month))
        deleteCashFlowInfo.delete()
        session.commit()


    def SearchCashFlowByMonthDataType(self, year, month, dataType):
        '''
        函数功能：以时间和数据类型为条件进行查询
        :param year:
        :param month:
        :return:
        '''

        balanceSheetInfo = session.query(CashFlowAlchemy).filter(and_(CashFlowAlchemy.year == year,
                                                                          CashFlowAlchemy.month == month,
                                                                          CashFlowAlchemy.data_type == dataType,
                                                                          )).all()

        return balanceSheetInfo
def main():

    cashFlowAlchemy = CashFlowAlchemy()
    cashFlowAlchemy.AddCashFlowItem()


CashFlowAlchemy.metadata.create_all(init.engine)
if __name__ == '__main__':
    main()